﻿using System;
using System.Windows;
using System.Windows.Input;
using WickedSick.ClientComponents.Music;
using System.Windows.Controls;
using System.Windows.Interop;
using System.Windows.Media;
using System.Xml.Serialization;

namespace SDB
{
	/// <summary>
	/// Interaction logic for Window1.xaml
	/// </summary>
	public partial class Window1 : Window
	{
        private MediaBI media;
        private Login wndLogin;

		public Window1()
		{
			this.InitializeComponent();
			
            this.Loaded += new RoutedEventHandler(Window1_Loaded);
            this.Closing += new System.ComponentModel.CancelEventHandler(Window1_Closing);
            SDB_Player.SongManager.MediaChanged += new MediaChangedDelegate(SongManager_MediaChanged);

            ContextMenu menu = FindResource("MyContext") as ContextMenu;
		}

        #region WINDOW EVENTS
        
        private void Window1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            media.LogoffUser(wndLogin.LoginGuid);
            //Show task dialog --> Logging off...
        }

        private void Window1_Loaded(object sender, RoutedEventArgs e)
        {
            string local_dir = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
            local_dir = local_dir.TrimEnd('\\');
            string media_file = local_dir + "\\media.sdb";
            media = new MediaBI(media_file);

            wndLogin = new Login(media);
            WindowInteropHelper whLogin = new WindowInteropHelper(wndLogin);
            whLogin.Owner = (new WindowInteropHelper(this)).Handle;
            wndLogin.ShowDialog();

            media.CommunicationComplete += new CommunicationCompleteDelegate(media_CommunicationComplete);
            media.UserLogoffComplete += new UserLogoffCompleteDelegate(media_UserLogoffComplete);
            if (media.Bank.Artists.Count == 0)
            {
                media.InitializeMedia(1);
            }
            else
            {
                SongTree.ItemsSource = media.Bank.Artists.Values;
            }
            lstPlaylist.ItemsSource = SDB_Player.SongManager.SongList;
        }

        private void lstPlaylist_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            int selected = ((PlaylistListBox)sender).SelectedIndex;
            SDB_Player.SongManager.PlaySongInPlaylist(selected);
        }

        private void lstPlaylist_Drop(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(typeof(Song)))
            {
                Song dragObject = e.Data.GetData(typeof(Song)) as Song;
                SDB_Player.SongManager.EnqueueSong(dragObject);
            }
            else if (e.Data.GetDataPresent(typeof(Album)))
            {
                Album dragObject = e.Data.GetData(typeof(Album)) as Album;
                foreach (Song song in dragObject.Songs.Values)
                {
                    SDB_Player.SongManager.EnqueueSong(song);
                }
            }
            else if (e.Data.GetDataPresent(typeof(Artist)))
            {
                Artist dragObject = e.Data.GetData(typeof(Artist)) as Artist;
                foreach (Album album in dragObject.Albums.Values)
                {
                    foreach (Song song in album.Songs.Values)
                    {
                        SDB_Player.SongManager.EnqueueSong(song);
                    }
                }
            }
        }

        private void SongTree_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                UIElement element = (UIElement)SongTree.InputHitTest(e.GetPosition(SongTree));
                while (element != SongTree)
                {
                    if (element is TreeViewItem)
                    {
                        object item = ((TreeViewItem)element).Header;
                        if (item is Song)
                        {
                            Song song = item as Song;
                            DataObject dragObject = new DataObject(typeof(Song), song);
                            DragDrop.DoDragDrop(SongTree, dragObject, DragDropEffects.Copy);
                        }
                        else if (item is Album)
                        {
                            Album album = item as Album;
                            DataObject dragObject = new DataObject(typeof(Album), album);
                            DragDrop.DoDragDrop(SongTree, dragObject, DragDropEffects.Copy);
                        }
                        else if (item is Artist)
                        {
                            Artist artist = item as Artist;
                            DataObject dragObject = new DataObject(typeof(Artist), artist);
                            DragDrop.DoDragDrop(SongTree, dragObject, DragDropEffects.Copy);
                        }
                        break;
                    }
                    element = (UIElement)VisualTreeHelper.GetParent(element);
                }
            }
        }

        #endregion

        private void SongManager_MediaChanged(object sender, MediaChangedEventArgs e)
        {
            try
            {
                PlaylistListBoxItem item = (PlaylistListBoxItem)lstPlaylist.ItemContainerGenerator.ContainerFromIndex(e.OldIndex);
                item.IsPlaying = false;
            } catch (Exception) { }

            try
            {
                PlaylistListBoxItem item2 = (PlaylistListBoxItem)lstPlaylist.ItemContainerGenerator.ContainerFromIndex(e.NewIndex);
                item2.IsPlaying = true;
            } catch (Exception) { }
        }

        private void media_CommunicationComplete(object sender, int CommunicationType)
        {
            media.SendMediaToFile();
            SongTree.ItemsSource = media.Bank.Artists.Values;
        }

        private void media_UserLogoffComplete(object sender)
        {
            Application.Current.Shutdown();
        }
	}
}